package cn.mxz.robots.province;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 吉林高考查询
 * 排名：完成
 * 去年排名：完成
 * 
 * @author chenmin <chenmin@mouge.wang>
 * @since 2015-06-23
 */
public class Jilin extends Exam implements IRank
{

	public Jilin(Student student)
	{
		super(student);
	}

	public static void main(String[] args)
	{
		Student student = new Student();
		student.setStudentId("15220182154489");
		student.setStudentPwd("宋永佳");
		student.setType("w");
		Jilin guangxi = new Jilin(student);
		System.out.println(guangxi.login(""));
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://www.jledu.gov.cn/chengji_2015.php?ksh=" + mStudent.getStudentId()
				+ "&xm=" + urlChineseEncode(mStudent.getStudentPwd()));
		String loginHtml = curlGet(loginRequest);

		JSONObject resultObject = new JSONObject();
		if (loginHtml.contains("语文"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("您要查询的结果没有找到"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document resultDoc = Jsoup.parse(parseHtml);
		Elements tables = resultDoc.select("table");

		JSONObject json = new JSONObject();
		json.put("exam_id", tables.get(0).select("tr").get(0).select("td").get(1).text());
		json.put("name", tables.get(0).select("tr").get(1).select("td").get(1).text());
		json.put("ranking",
				getRank(15, tables.get(1).select("tr").get(5).select("td").get(1).text(), mStudent.getType()));
		json.put("province", "吉林省");

		JSONObject score = new JSONObject();
		score.put("语文", tables.get(1).select("tr").get(0).select("td").get(1).text());
		score.put("数学", tables.get(1).select("tr").get(1).select("td").get(1).text());
		score.put("综合", tables.get(1).select("tr").get(2).select("td").get(1).text());
		score.put("外语", tables.get(1).select("tr").get(3).select("td").get(1).text());
		score.put("民族语文", tables.get(1).select("tr").get(4).select("td").get(1).text());
		score.put("总成绩", tables.get(1).select("tr").get(5).select("td").get(1).text());
		json.put("score", score.toString());

		return json.toString();
	}

	@Override
	public void initRank()
	{
		// 文
		// http://www.chinadaily.com.cn/dfpd/dfjyzc/2015-06-23/content_13873105.html
		// 理 http://edu.163.com/15/0623/10/ASPO9H4600294M9N.html
		Request request = new Request("http://edu.163.com/15/0623/10/ASPO9H4600294M9N.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));

		Integer counter = 0;
		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 1; i < trs.size(); i++)
		{
			Elements tds = trs.get(i).select("td");
			for (int j = 1; j < tds.size(); j++)
			{
				Integer score = Integer.valueOf(tds.get(0).text()) + (9 - j + 1);

				if (j == 1)
				{
					counter = Integer.valueOf(tds.get(j).text()) - 0;
				}
				else
				{
					counter = Integer.valueOf(tds.get(j).text()) - Integer.valueOf(tds.get(j - 1).text());
				}

				Integer ranking = Integer.valueOf(tds.get(j).text());
				System.out.println(score + ":" + counter + ":" + ranking);

				GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
				GaoKaoProvince province = provinceService.get(15 + 31);
				GaoKaoRankService rankService = new GaoKaoRankService();
				System.out.println(score);
				GaoKaoRank rank = new GaoKaoRank();
				rank.setScore(score);
				rank.setCount(counter);
				rank.setType("l");
				rank.setRanking(ranking);
				rank.setGaoKaoProvince(province);
				rankService.save(rank);
			}

		}
	}

	@Override
	public void lastYearRank()
	{
		// 文 http://www.gxeduw.com/gaokao/2014/106925.html
		// 理 http://www.gxeduw.com/gaokao/2014/106924.html
		Request request = new Request("http://www.gxeduw.com/gaokao/2014/106925.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));

		Integer counter = 0;
		Integer number = 77;
		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 2; i < trs.size(); i++)
		{
			Elements tds = trs.get(i).select("td");
			for (int j = 1; j < tds.size(); j++)
			{
				Integer score = Integer.valueOf(tds.get(0).text()) + (9 - j + 1);

				if (tds.get(j).text().equals(" ") || tds.get(j - 1).text().equals(" "))
					continue;

				counter = Integer.valueOf(tds.get(j).text()) - number;
				number = Integer.valueOf(tds.get(j).text());

				Integer ranking = Integer.valueOf(tds.get(j).text());
				System.out.println(score + ":" + counter + ":" + ranking);

				GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
				GaoKaoProvince province = provinceService.get(15);
				GaoKaoRankService rankService = new GaoKaoRankService();
				GaoKaoRank rank = new GaoKaoRank();
				rank.setScore(score);
				rank.setCount(counter);
				rank.setType(WEN_KE);
				rank.setRanking(ranking);
				rank.setGaoKaoProvince(province);
				rankService.save(rank);
			}
		}
	}

}
